package com.hhkj.admin.order.domain;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.hhkj.admin.order.common.valid.AgreeRefund;
import com.hhkj.admin.order.common.valid.RefundPayout;
import com.ruoyi.common.core.domain.BizBaseEntity;
import lombok.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 记录客户每笔退款的信息
 */
@EqualsAndHashCode(callSuper = true)
@TableName(value = "s_order_refund")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class OrderRefund extends BizBaseEntity {

    /** 退款单号 */
    @TableField(value = "num")
    private String num;

    /** 退款状态："refunding"-退款中 "success"-退款成功 "failed"-退款失败 "cancel"-已取消 */
    @TableField(value = "status_code")
    private String statusCode;

    /** 开始时间 */
    @TableField(value = "start_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;

    /** 结束时间 */
    @TableField(value = "end_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

    /** 打款时间 */
    @TableField(value = "payout_date")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @NotNull(message = "打款时间不能为空", groups = {RefundPayout.class})
    private Date payoutDate;

    /** 退款金额 */
    @TableField(value = "price")
    private BigDecimal price;

    /** 退款金额 */
    @TableField(value = "final_price")
    @NotNull(message = "实际退款金额不能为空", groups = {RefundPayout.class})
    private BigDecimal finalPrice;

    /** 学员数量 */
    @TableField(value = "student_amount")
    private Integer studentAmount;

    /** 审核结果说明 */
    @TableField(value = "audit_description")
    @NotNull(message = "审核说明不能为空", groups = {AgreeRefund.class})
    private String auditDescription;

    /** 打款状态："pending"-待打款 "done"-已打款 */
    @TableField(value = "payout_status_code")
    private String payoutStatusCode;

    /** 账户名称 */
    @TableField(value = "account_name")
    private String accountName;

    /** 账户账号 */
    @TableField(value = "account")
    private String account;

    /** 退款说明 */
    @TableField(value = "description")
    private String description;

    /** 订单号，关联s_order */
    @TableField(value = "order_id")
    @NotNull(message = "订单id不能为空", groups = {AgreeRefund.class, RefundPayout.class})
    private Long orderId;

    /** 订单号，关联s_order */
    @TableField(value = "payout_receipt_url")
    @NotNull(message = "打款凭证不能为空", groups = {RefundPayout.class})
    private String payoutReceiptUrl;

    /** 微信退款订单单号 */
    private String wechatRefundId;

    public void setFinalPrice(BigDecimal finalPrice) {
        if (ObjectUtil.isNotEmpty(finalPrice)) {
            this.finalPrice = finalPrice.setScale(0, BigDecimal.ROUND_DOWN);
        }
    }
}